package com.snatch.snatchmgr.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.snatch.common.constant.SnatchConstant;
import com.snatch.common.utils.DateUtil;
import com.snatch.common.utils.SnatchConstantUtil;
import com.snatch.snatchmgr.dto.PlayinfoDto;
import com.snatch.snatchmgr.dto.ThirdItemDto;
import com.snatch.snatchmgr.dto.ThirdMatchDto;
import com.snatch.snatchmgr.entity.db.ThirdLive;
import com.snatch.snatchmgr.entity.db.ThirdMatch;
import com.snatch.snatchmgr.entity.db.ThirdPlayinfo;
import com.snatch.snatchmgr.entity.db.ThirdTradingItem;
import com.snatch.snatchmgr.service.AdminDataService;
import com.snatch.snatchmgr.system.SystemData;
import com.snatch.snatchmgr.vo.League;
import com.snatch.snatchmgr.vo.Match;

/**
 * 智能匹配数据服务类
 */
@Service
public class AdminDataServiceImpl implements AdminDataService {
	// 日志对象
	private Logger log = Logger.getLogger(AdminDataServiceImpl.class);

	@Override
	public List<League> findLeagues(String webCode, String gameCode,String gameType) {
		List<League> list = new ArrayList<League>();
		Map<String,League> leaguemap = new HashMap<String,League>();
		Map<String, ThirdMatch> map = SystemData.findThirdMatchs(webCode, gameCode,gameType);
		if(null == map) return list;
		Iterator<ThirdMatch> iterator = map.values().iterator();
		while (iterator.hasNext()) {
			ThirdMatch thirdMatch = iterator.next();
			if(thirdMatch.getMatchType().equals(gameType)){
				if(!leaguemap.containsKey(thirdMatch.getLeagueSelfid())){
					League league = new League();
					league.setLeagueName(thirdMatch.getLeagueName());
					league.setLeagueSelfID(thirdMatch.getLeagueSelfid());
					leaguemap.put(thirdMatch.getLeagueSelfid(), league);
				}
			}
		}
		list = new ArrayList<League>(leaguemap.values());  
		leaguemap.clear();
		leaguemap = null;
		return list;
	}

	@Override
	public List<Match> findMatchs(String webCode, String gameCode,String gameType,
			String leagueid) {
		List<Match> list = new ArrayList<Match>();
		Map<String, ThirdMatch> map = SystemData.findThirdMatchs(webCode, gameCode,gameType);
		if(null == map) return list;
		Iterator<ThirdMatch> iterator = map.values().iterator();
		while (iterator.hasNext()) {
			ThirdMatch thirdMatch = iterator.next();
			if(thirdMatch.getLeagueSelfid().equals(leagueid)){
				Match match = new Match();
				match.setMatchName(thirdMatch.getMatchName());
				match.setId(String.valueOf(thirdMatch.getId()));
				match.setMatchSelfID(thirdMatch.getMatchSelfid());
				match.setMatchType(thirdMatch.getMatchType());
				match.setMatchTypeText(SnatchConstantUtil.changeMatchTypeName(thirdMatch.getMatchType()));
				list.add(match);
			}
		}
		return list;
	}

	@Override
	public ThirdMatchDto findThirdMatch(String webCode, String gameCode,String gameType,
			String leagueid, String matchid) {
		ThirdMatchDto temthirdMatch = new ThirdMatchDto();
		Map<String, ThirdMatch> map = SystemData.findThirdMatchs(webCode, gameCode,gameType);
		if(null == map) return null;
		ThirdMatch thirdMatch = map.get(SystemData.getMatchMapKey(leagueid, matchid));
		if (thirdMatch != null && thirdMatch.getMatchType().equals(gameType)) {
			temthirdMatch.setMatchName(thirdMatch.getMatchName());
			temthirdMatch.setMatchSelfID(thirdMatch.getMatchSelfid());
			temthirdMatch.setStartTime(DateUtil.format(thirdMatch.getStartTime(), SnatchConstant.MATCH_START_TIME));
			for (ThirdPlayinfo npi : thirdMatch.getThirdPlayinfos().values()) {
				if(npi.isValid()){
					PlayinfoDto newNpi = new PlayinfoDto();
					newNpi.setName(buildPIName(npi));
					newNpi.setBunch(npi.isBunch());
					for (ThirdTradingItem tii : npi.getThirdTradingItems().values()) {
						ThirdItemDto tid = new ThirdItemDto();
						tid.setName(tii.getName());
						tid.setId(tii.getId());
						tid.setRatio(tii.getRatio());
						newNpi.getTradeItemList().add(tid);
					}
					temthirdMatch.getPlayinfoDtoList().add(newNpi);
				}
			}
		}
		return temthirdMatch;
		
	}
	
	@Override
	public ThirdLive findLiveData(String webCode, String gameCode, String livekey) {
		ThirdLive liveData = SystemData.findLiveData(webCode, gameCode, livekey);
		if(null == liveData) return null;
		return liveData.clone();
	}

	private String buildPIName(ThirdPlayinfo npi) {
		StringBuffer name = new StringBuffer();
		name.append(SystemData.getTradePlayType(npi.getGametime())).append(" ").append(SystemData.getChinesePlayInfo(npi.getName()));
		if(null != npi.getHandicap()){
			name.append(" ").append(npi.getHandicap());
		}
		return name.toString();
	}

	
	

}
